/**
 * Copyright (c) 2013-Now http://jeesite.com All rights reserved.
 */
package com.jeesite.modules.biz.web;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.jeesite.common.config.Global;
import com.jeesite.common.entity.Page;
import com.jeesite.common.idgen.IdGen;
import com.jeesite.common.lang.StringUtils;
import com.jeesite.common.mapper.JsonMapper;
import com.jeesite.common.web.BaseController;
import com.jeesite.modules.biz.entity.BizGoods;
import com.jeesite.modules.biz.service.BizGoodsService;

/**
 * 物料表Controller
 * @author Lai_Hanzhang
 * @version 2019-03-11
 */
@Controller
@RequestMapping(value = "${adminPath}/biz/bizGoods")
public class BizGoodsController extends BaseController {

	@Autowired
	private BizGoodsService bizGoodsService;
	
	/**
	 * 获取数据
	 */
	@ModelAttribute
	public BizGoods get(String goodsId, boolean isNewRecord) {
		return bizGoodsService.get(goodsId, isNewRecord);
	}
	
	/**
	 * 查询列表
	 */
	@RequiresPermissions("biz:bizGoods:view")
	@RequestMapping(value = {"list", ""})
	public String list(BizGoods bizGoods, Model model) {
		model.addAttribute("bizGoods", bizGoods);
		return "modules/biz/bizGoodsList";
	}
	
	/**
	 * 查询列表数据
	 */
	@RequiresPermissions("biz:bizGoods:view")
	@RequestMapping(value = "listData")
	@ResponseBody
	public Page<BizGoods> listData(BizGoods bizGoods, HttpServletRequest request, HttpServletResponse response) {
		bizGoods.setPage(new Page<>(request, response));
		Page<BizGoods> page = bizGoodsService.findPage(bizGoods);
		return page;
	}
	
	/**
	 * 查询所有数据
	 */
	@RequiresPermissions("biz:bizGoods:view")
	@RequestMapping(value = "listAllData")
	@ResponseBody
	public String listAllData(BizGoods bizGoods, HttpServletRequest request, HttpServletResponse response) {
		return JsonMapper.toJson(bizGoodsService.findAlldata(bizGoods)) ;
	}

	/**
	 * 查看编辑表单
	 */
	@RequiresPermissions("biz:bizGoods:view")
	@RequestMapping(value = "form")
	public String form(BizGoods bizGoods, Model model) {
		if(StringUtils.isBlank(bizGoods.getId())){
		    String bizCode = IdGen.nextId();
		    bizGoods.setGoodsCode(bizCode);
		}
		model.addAttribute("bizGoods", bizGoods);
		return "modules/biz/bizGoodsForm";
	}

	/**
	 * 保存物料表
	 */
	@RequiresPermissions("biz:bizGoods:edit")
	@PostMapping(value = "save")
	@ResponseBody
	public String save(@Validated BizGoods bizGoods) {
		bizGoodsService.save(bizGoods);
		return renderResult(Global.TRUE, text("保存物料表成功！"));
	}
	
	/**
	 * 停用物料表
	 */
	@RequiresPermissions("biz:bizGoods:edit")
	@RequestMapping(value = "disable")
	@ResponseBody
	public String disable(BizGoods bizGoods) {
		bizGoods.setStatus(BizGoods.STATUS_DISABLE);
		bizGoodsService.updateStatus(bizGoods);
		return renderResult(Global.TRUE, text("停用物料表成功"));
	}
	
	/**
	 * 启用物料表
	 */
	@RequiresPermissions("biz:bizGoods:edit")
	@RequestMapping(value = "enable")
	@ResponseBody
	public String enable(BizGoods bizGoods) {
		bizGoods.setStatus(BizGoods.STATUS_NORMAL);
		bizGoodsService.updateStatus(bizGoods);
		return renderResult(Global.TRUE, text("启用物料表成功"));
	}
	
	/**
	 * 删除物料表
	 */
	@RequiresPermissions("biz:bizGoods:edit")
	@RequestMapping(value = "delete")
	@ResponseBody
	public String delete(BizGoods bizGoods) {
		bizGoodsService.delete(bizGoods);
		return renderResult(Global.TRUE, text("删除物料表成功！"));
	}
	
}